using Sirenix.OdinInspector;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.Events;

public class 立库_堆垛机 : DeviceMono
{
    public Up up;
    //public Animation animation;//无法使用，已换成Animator
    public Animator 过弯_animator;
    public Transform mainParent;//主父级

    [LabelText("目标列层排")]
    public Vector3 taskLoction;//任务位置

    [LabelText("当前列层排")]
    public Vector3 thisLoction;//当前位置

    Vector3 tasksVector3;//当前目标位置
    StorageLocation targetSL;//目标储位

    Vector3 leftTurn = new Vector3(4, 1, 16);//左转向位

    internal bool thisTaskLoction_堆垛;//是否已到任务位置
    internal bool thisTaskLoction_升降;//是否已到任务位置
    [Title("伸缩叉")]
    public Transform 伸缩叉_1;//伸缩叉一段
    public Transform 伸缩叉_2;
    [FoldoutGroup("伸缩叉_伸缩点")]
    [LabelText("伸缩叉一_初始点")]
    public Vector3 stage0_0;//伸缩叉一阶段
    [FoldoutGroup("伸缩叉_伸缩点")]
    [LabelText("伸缩叉_左伸一段")]
    public Vector3 stageLeft_1;//伸缩叉一阶段
    [FoldoutGroup("伸缩叉_伸缩点")]
    [LabelText("伸缩叉_左伸二段")]
    public Vector3 stageLeft_2;//伸缩叉二阶段
    [FoldoutGroup("伸缩叉_伸缩点")]
    [LabelText("伸缩叉_左伸三段")]
    public Vector3 stageLeft_3;//伸缩叉三阶段
    [FoldoutGroup("伸缩叉_伸缩点")]
    [LabelText("伸缩叉_左伸四段")]
    public Vector3 stageLeft_4;//伸缩叉三阶段
    [FoldoutGroup("伸缩叉_伸缩点")]
    [LabelText("伸缩叉二_初始点")]
    public Vector3 stage1_0;//伸缩叉一阶段
    [FoldoutGroup("伸缩叉_伸缩点")]
    [LabelText("伸缩叉_右伸一段")]
    public Vector3 stageRight_1;//伸缩叉一阶段
    [FoldoutGroup("伸缩叉_伸缩点")]
    [LabelText("伸缩叉_右伸二段")]
    public Vector3 stageRight_2;//伸缩叉二阶段
    [FoldoutGroup("伸缩叉_伸缩点")]
    [LabelText("伸缩叉_右伸三段")]
    public Vector3 stageRight_3;//伸缩叉三阶段
    [FoldoutGroup("伸缩叉_伸缩点")]
    [LabelText("伸缩叉_右伸四段")]
    public Vector3 stageRight_4;//伸缩叉三阶段

    public float speed;//堆垛机速度
    public float speed_sj;//升降速度

    public Transform jiaozhundian;
    public Transform duiduoji;
    [LabelText("升降结构")]
    public Transform liftingStructure;//升降结构

    internal Vector3 targetPoint_V3 = new Vector3();

    public 堆垛机过弯 堆垛机过弯动画;
    internal List<Action> actions;
    int judge_过弯;//0：左转，1：右转

    int dataIndexMessage;
    void Start()
    {
    }

    private void Update()
    {
    }

    internal override void DeviceMontoring(string[] dataStrArray)
    {
        if (movement_List.Contains(dataStrArray[0]))//判断是否为当前设备数据
        {
            dataIndexMessage = movement_List.FindIndex(x=>(x == dataStrArray[0]));
            if (dataIndexMessage == 0)
            {

            }
        }
    }
    public void InsMysql()
    {

    }

    //伸缩叉运动
    public void 伸缩叉_Move()
    {

    }


    public void ScheduleTasks(Vector3 tasksVector3Tool) //进行任务安排
    {
        if (taskLoction == thisLoction)
        {
            return;
        }

        if (tasksVector3Tool.z == 0)//判断是否是站台(排为0时)
        {
            Debug.Log("停靠站台");
            if (deciveID == "4")//判断是否为1号堆垛机（一号堆垛机有两个入口站台）
            {
                if (tasksVector3Tool.z == 1 || tasksVector3Tool.z == 2)
                {
                    //Move_DuiDuo(DepotMain.instances.站台_All[0]);
                }
                else
                {
                    //Move_DuiDuo(DepotMain.instances.站台_All[1]);
                }
            }
            else if (deciveID == "5" && taskLoction.z == 0)//排为0时
            {
                //Move_DuiDuo(DepotMain.instances.站台_All[2]);
            }
            else if (deciveID == "6"&&taskLoction.z == 0) 
            {
                //Move_DuiDuo(DepotMain.instances.站台_All[3]);
            }
        }

        taskLoction = tasksVector3Tool;
        thisLoction = taskLoction;
        judge_过弯 = Judge_过弯();
        if (judge_过弯 != -1)//需要过弯
        {
            Debug.Log("堆垛机需要过弯");
            if (judge_过弯 == 0)//左转
            {
                //移动到指定位置后进行过弯（过弯后自动移动至任务位置）
                Move_DuiDuo(GetTarget(leftTurn).transform, PositiveAnimetor过弯_Event);
            }
            else
                Move_DuiDuo(GetTarget(tasksVector3).transform, NegativeAnimetor过弯_Event);
        }
        else//无需过弯
        {
            Debug.Log("堆垛机无需过弯");
            Move_DuiDuo(GetTarget(taskLoction).transform);//堆垛机到达任务位置
        }
    }   
    int Judge_过弯() //判断是否需要过弯
    {
        Debug.Log("过弯：" + thisLoction.x + "  " + taskLoction.x);
        if ((thisLoction.x == 3 || thisLoction.x == 4))//通过列判断是是否需要过弯
        {
            if (taskLoction.x == 1 || taskLoction.x == 2)
            {
                return 0;
            }
        }
        else if ((thisLoction.x == 1 || thisLoction.x == 2))
        {
            if (taskLoction.x == 3 || taskLoction.x == 4)
            {
                return 1;
            }
        }
        return -1;
    }

    //获取目标储位x:cloumn,y:layrer,z:row:
    StorageLocation GetTarget(Vector3 storageV3)
    {
        //if (deciveID == "4")
        //{
        //    return DepotMain.instances.storageLocation铝杆_All.Find(
        //    x => x.storageVector3 == storageV3
        //    );
        //}
        //else if (deciveID == "5")
        //{
        //    return DepotMain.instances.storageLocation铝锭一区_All.Find(
        //    x => x.storageVector3 == storageV3
        //     );
        //}
        //else
        //{
        //    return DepotMain.instances.storageLocation铝锭二区_All.Find(
        //    x => x.storageVector3 == storageV3
        //    );
        //}
        return new StorageLocation();
    }

    //堆垛机移动
    public void Move_DuiDuo(Vector3 tagPoint)
    {
        targetPoint_V3.x = tagPoint.x;
        targetPoint_V3.y = transform.localPosition.y;
        targetPoint_V3.z = transform.localPosition.z;
        StartCoroutine(MotionManager.instance.MoveObj_IE(
            transform, targetPoint_V3, 0.1f));
        targetPoint_V3.x = liftingStructure.localPosition.x;
        targetPoint_V3.y = liftingStructure.localPosition.y;
        targetPoint_V3.z = tagPoint.y;
        StartCoroutine(MotionManager.instance.MoveObj_IE(
            liftingStructure, targetPoint_V3, 0.1f));
    }

    public void Move_DuiDuo(Transform tagTransform, Action action = null, Action action1 = null)
    {
        targetPoint_V3.x = tagTransform.localPosition.x;
        targetPoint_V3.y = transform.localPosition.y;
        targetPoint_V3.z = transform.localPosition.z;
        StartCoroutine(MotionManager.instance.MoveObj_IE(
            transform, targetPoint_V3, speed, action));
        //tagTransform.SetParent(transform);
        targetPoint_V3.y = tagTransform.position.y - liftingStructure.position.y;
        //升降机动作
        //StartCoroutine(MotionManager.instance.MoveObjWord_IE_Y(
        //    liftingStructure, tagTransform.position.y, targetPoint_V3.y, speed_sj,
        //    action1));
    }

    void ThisTaskLoction堆垛_Funtion()//堆垛到达指定位置后
    {

    }

    void ThisTaskLoction升降_Funtion() //升降到达指定位置后
    {

    }

    void ThisTaskLoction_Funtion() //堆垛机到达指定位置后
    {

    }

    public void PositiveAnimetor过弯_Event()//正播
    {
        Debug.Log("正播");
        堆垛机过弯动画.PlayAnimator_positive();
    }

    public void NegativeAnimetor过弯_Event()//倒播
    {
        Debug.Log("倒播");
        堆垛机过弯动画.PlayAnimator_negative();
    }

    public void StartAnimetor过弯_Funtion() //堆垛机开始过弯
    {
        transform.SetParent(堆垛机过弯动画.transform);
    }

    public void EndAnimetor过弯_Funtion() //堆垛机完成过弯
    {
        transform.SetParent(mainParent);
        Move_DuiDuo(GetTarget(taskLoction).transform);//堆垛机过弯后到达指定位置
    }

    [ContextMenu("wordPoint")]
    public void pointWord()
    {
        Debug.Log(jiaozhundian.position + "  " + duiduoji.position);
    }

    public void LeftRotation() //左转
    {

    }

    public void RightRotation()//右转
    {

    }

    public void 伸缩_Animtor(int leftRight)
    {
        if (deciveID == "4")//铝杆堆垛机
        {
            if (leftRight == 1 || leftRight == 4)//左伸
            {
                伸缩叉_AnimtorLeftLG_Funtion();
            }
            else
            {
                伸缩叉_AnimtorRightLG_Funtion();
            }

        }
        else if (deciveID == "5") //铝锭一区堆垛机
        {
            if (leftRight == 5)//左伸
            {
                伸缩叉_AnimtorLeftLD_Funtion_NearORfar(0);//近伸
            }
            else if (leftRight == 6) //左伸
            {
                伸缩叉_AnimtorLeftLD_Funtion_NearORfar(1);//远伸
            }
            else if (leftRight == 7) //右伸
            {
                伸缩叉_AnimtorRightLD_Funtion_NearORfar(0);//近伸
            }
            else if (leftRight == 8) //右伸
            {
                伸缩叉_AnimtorRightLD_Funtion_NearORfar(1);//远伸
            }
        }
        else //铝锭二区堆垛机
        {
            if (leftRight == 9)//左伸
            {
                伸缩叉_AnimtorLeftLD_Funtion_NearORfar(0);//近伸
            }
            else if (leftRight == 10) //左伸
            {
                伸缩叉_AnimtorLeftLD_Funtion_NearORfar(1);//远伸
            }
            else if (leftRight == 11) //右伸
            {
                伸缩叉_AnimtorRightLD_Funtion_NearORfar(0);//近伸
            }
            else if (leftRight == 12) //右伸
            {
                伸缩叉_AnimtorRightLD_Funtion_NearORfar(1);//远伸
            }
        }
    }

    [LabelText("伸缩叉速度_1")]
    public float speedStageSS_1;
    [LabelText("伸缩叉速度_2")]
    public float speedStageSS_2;
    [Button("左伸", ButtonSizes.Large, Style = ButtonStyle.Box),]
    public void 伸缩叉_AnimtorLeftLG_Funtion()//伸缩叉动画
    {
        Debug.Log("左伸");
        伸缩叉_1.transform.localPosition = stage0_0;
        伸缩叉_2.transform.localPosition = stage1_0;
        StartCoroutine(MotionManager.instance.MoveObj_IE(伸缩叉_1, stageLeft_1, speedStageSS_1,
            () =>
            {
                StartCoroutine(MotionManager.instance.MoveObj_IE(伸缩叉_1, stageLeft_2, speedStageSS_1));
                StartCoroutine(MotionManager.instance.MoveObj_IE(伸缩叉_2, stageLeft_3, speedStageSS_2));
            }));
    }
    [Button("右伸", ButtonSizes.Large, Style = ButtonStyle.Box),]
    void 伸缩叉_AnimtorRightLG_Funtion()//伸缩叉动画
    {
        Debug.Log("右伸");
        伸缩叉_1.transform.localPosition = stage0_0;
        伸缩叉_2.transform.localPosition = stage1_0;
        StartCoroutine(MotionManager.instance.MoveObj_IE(伸缩叉_1, stageRight_1, speedStageSS_1,
            () =>
            {
                StartCoroutine(MotionManager.instance.MoveObj_IE(伸缩叉_1, stageRight_2, speedStageSS_1));
                StartCoroutine(MotionManager.instance.MoveObj_IE(伸缩叉_2, stageRight_3, speedStageSS_2));
            }));
    }
    [Button("右伸", ButtonSizes.Large, Style = ButtonStyle.Box),]
    void 伸缩叉_AnimtorRightLD_Funtion_NearORfar(int nearORfar) 
    {
        if (nearORfar == 0)
        {
            Debug.Log("右伸");
            伸缩叉_1.transform.localPosition = stage0_0;
            伸缩叉_2.transform.localPosition = stage1_0;
            StartCoroutine(MotionManager.instance.MoveObj_IE(伸缩叉_1, stageRight_1, speedStageSS_1,
                () =>
                {
                    StartCoroutine(MotionManager.instance.MoveObj_IE(伸缩叉_2, stageRight_2, speedStageSS_1));
                }));
        }
        else 
        {
            Debug.Log("右伸");
            伸缩叉_1.transform.localPosition = stage0_0;
            伸缩叉_2.transform.localPosition = stage1_0;
            StartCoroutine(MotionManager.instance.MoveObj_IE(伸缩叉_1, stageRight_3, speedStageSS_1,
                () =>
                {
                    StartCoroutine(MotionManager.instance.MoveObj_IE(伸缩叉_2, stageRight_4, speedStageSS_1));
                }));
        }

       
    }
    [Button("左伸", ButtonSizes.Large, Style = ButtonStyle.Box),]
    public void 伸缩叉_AnimtorLeftLD_Funtion_NearORfar(int nearORfar)//伸缩叉动画
    {
        if (nearORfar == 0)
        {
            Debug.Log("左伸");
            伸缩叉_1.transform.localPosition = stage0_0;
            伸缩叉_2.transform.localPosition = stage1_0;
            StartCoroutine(MotionManager.instance.MoveObj_IE(伸缩叉_1, stageLeft_1, speedStageSS_1,
                () =>
                {
                    StartCoroutine(MotionManager.instance.MoveObj_IE(伸缩叉_2, stageLeft_2, speedStageSS_2));
                }));
        }
        else 
        {
            Debug.Log("左伸");
            伸缩叉_1.transform.localPosition = stage0_0;
            伸缩叉_2.transform.localPosition = stage1_0;
            StartCoroutine(MotionManager.instance.MoveObj_IE(伸缩叉_1, stageLeft_3, speedStageSS_1,
                () =>
                {
                    StartCoroutine(MotionManager.instance.MoveObj_IE(伸缩叉_2, stageLeft_4, speedStageSS_2));
                }));
        }
    }
}
[Serializable]
public class Up : UnityEvent<KeyCode> { }
